@echo off
chcp 65001 >nul
cls
color 0B

echo.
echo ════════════════════════════════════════════
echo   选择设备并诊断模拟位置问题
echo ════════════════════════════════════════════
echo.

echo 检测到的设备：
echo.
adb devices
echo.

echo 请选择设备：
echo   1. 4f6c2e31 (真机)
echo   2. emulator-5554 (模拟器)
echo.

set /p choice=输入选项 (1 或 2): 

if "%choice%"=="1" (
    set DEVICE=4f6c2e31
    echo.
    echo 已选择：真机 4f6c2e31
) else if "%choice%"=="2" (
    set DEVICE=emulator-5554
    echo.
    echo 已选择：模拟器 emulator-5554
) else (
    echo 无效选项
    pause
    exit /b 1
)

echo.
echo ════════════════════════════════════════════
echo   开始诊断
echo ════════════════════════════════════════════
echo.

echo [1/6] 检查模拟位置权限...
echo.
adb -s %DEVICE% shell appops get com.example.munidingwei MOCK_LOCATION
echo.

echo [2/6] 检查应用是否在运行...
echo.
adb -s %DEVICE% shell ps | findstr munidingwei
if errorlevel 1 (
    echo ⚠️ 应用未运行
) else (
    echo ✅ 应用正在运行
)
echo.

echo [3/6] 检查前台服务...
echo.
adb -s %DEVICE% shell dumpsys activity services com.example.munidingwei/.LocationForegroundService
echo.

echo [4/6] 检查Mock Provider...
echo.
adb -s %DEVICE% shell dumpsys location | findstr -i "mock\|test\|provider"
echo.

echo [5/6] 最近的应用日志...
echo.
echo === LocationService ===
adb -s %DEVICE% logcat -d -s LocationService:D | findstr /C:"模拟" /C:"Mock" /C:"位置"
echo.
echo === LocationForegroundService ===
adb -s %DEVICE% logcat -d -s LocationForegroundService:D | findstr /C:"更新" /C:"前台"
echo.
echo === MockLocationProvider ===
adb -s %DEVICE% logcat -d -s MockLocationProvider:D | findstr /C:"Mock" /C:"provider"
echo.

pause

echo.
echo [6/6] 实时监控...
echo.
echo 📱 现在请在手机上操作：
echo    1. 搜索地址或拖动地图
echo    2. 点击"设置为模拟位置"
echo    3. 观察日志输出
echo.
echo 按 Ctrl+C 停止
echo.

adb -s %DEVICE% logcat -c
adb -s %DEVICE% logcat LocationService:D LocationForegroundService:D MockLocationProvider:D *:S

pause

